package com.tianji.learning.listener;

import com.tianji.api.dto.leanring.PointsRecordMsgDTO;
import com.tianji.common.utils.JsonUtils;
import com.tianji.learning.service.IPointsRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.LEARNING_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.POINT_RECORD_KEY;
import static com.tianji.common.constants.MqConstants.Queue.POINTS_RECORD_QUEUE;

@Slf4j
@Component
public class PointsRecordChangeListener {

    @Autowired
    private IPointsRecordService recordService;

    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name=POINTS_RECORD_QUEUE),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key=POINT_RECORD_KEY
    ))
    public void onPointsRecordChange(PointsRecordMsgDTO msg){
        log.info("收到积分变更消息，msg：{}", JsonUtils.toJsonStr(msg));
        recordService.onPointsRecordChange(msg);
    }

}
